﻿using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.Security;
using DevExpress.Persistent.BaseImpl.PermissionPolicy;
using DevExpress.Persistent.Base.Security;
using Tida.Module.BusinessObjects.组织架构;
using Tida.Module.BusinessObjects.研发中心;

namespace Tida.Module.BusinessObjects.系统管理
{
    [NavigationItem("组织架构")]
    [XafDisplayName("用户")]
    [DefaultProperty("姓名")]

    public class Sys_User : BaseInf, ISecurityUser, IAuthenticationStandardUser, IAuthenticationActiveDirectoryUser, ISecurityUserWithRoles, IPermissionPolicyUser, ICanInitialize
    { // Inherit from a different class to provide a custom primary key, concurrency and deletion behavior, etc. (https://documentation.devexpress.com/eXpressAppFramework/CustomDocument113146.aspx).
        public Sys_User(Session session)
            : base(session)
        {

        }

        public override void AfterConstruction()
        {

            base.AfterConstruction();
            SetPassword("123456");
            var oo = new XPQuery<Sys_Role>(this.Session);
            Sys_Role ro = oo.Where(m => m.Name == "普通员工").FirstOrDefault();
            this.Sys_Roles.Add(ro);

            // Place your initialization code here (https://documentation.devexpress.com/eXpressAppFramework/CustomDocument112834.aspx).
        }


        [Association("Sys_User-TaskWithNotifications")]
        public XPCollection<TaskWithNotifications> TaskWithNotifications
        {
            get
            {
                return GetCollection<TaskWithNotifications>("TaskWithNotifications");
            }

        }
     



        #region ISecurityUser Members

        private string userName = String.Empty;
        [RuleRequiredField("Sys_UserUserNameRequired", DefaultContexts.Save)]


        [RuleUniqueValue("用户名必须是唯一，不能存在多个相同工号", DefaultContexts.Save,
         CriteriaEvaluationBehavior = CriteriaEvaluationBehavior.BeforeTransaction)]
        [XafDisplayName("用户名")]
        public string UserName
        {
            get { return userName; }
            set { SetPropertyValue("UserName", ref userName, value); }
        }
        [XafDisplayName("是否可用")]
        private bool isActive = true;
        public bool IsActive
        {
            get { return isActive; }
            set { SetPropertyValue("IsActive", ref isActive, value); }
        }
        #endregion

        #region IAuthenticationStandardUser Members
        private bool changePasswordOnFirstLogon;
        [XafDisplayName("下次登录更改密码")]
        public bool ChangePasswordOnFirstLogon
        {
            get { return changePasswordOnFirstLogon; }
            set
            {
                SetPropertyValue("ChangePasswordOnFirstLogon", ref changePasswordOnFirstLogon, value);
            }
        }
        private string storedPassword;
        [Browsable(false), Size(SizeAttribute.Unlimited), Persistent, SecurityBrowsable]
        protected string StoredPassword
        {
            get { return storedPassword; }
            set { storedPassword = value; }
        }
        public bool ComparePassword(string password)
        {
            return PasswordCryptographer.VerifyHashedPasswordDelegate(this.storedPassword, password);
        }
        public void SetPassword(string password)
        {
            this.storedPassword = PasswordCryptographer.HashPasswordDelegate(password);
            OnChanged("StoredPassword");
        }
        #endregion


        #region ISecurityUserWithRoles Members
        IList<ISecurityRole> ISecurityUserWithRoles.Roles
        {
            get
            {
                IList<ISecurityRole> result = new List<ISecurityRole>();
                foreach (Sys_Role role in Sys_Roles)
                {
                    result.Add(role);
                }
                return result;
            }
        }
        #endregion

        [Association("Sys_Users-Sys_Roles")]
        [RuleRequiredField("Sys_RoleIsRequired", DefaultContexts.Save,
            TargetCriteria = "IsActive",
            CustomMessageTemplate = "An active Sys_User must have at least one role assigned")]
        public XPCollection<Sys_Role> Sys_Roles
        {
            get
            {
                return GetCollection<Sys_Role>("Sys_Roles");
            }
        }

        private string _分机号码;
        private string _短号;
        private 岗位 _岗位;
        private 部门 _部门;
        [RuleUniqueValue("员工工号必须是唯一的，不能存在多个相同工号", DefaultContexts.Save,
        CriteriaEvaluationBehavior = CriteriaEvaluationBehavior.BeforeTransaction)]
        [Size(SizeAttribute.DefaultStringMappingFieldSize)]
        public string 员工工号
        {
            get
            {
                return _员工工号;
            }
            set
            {
                SetPropertyValue("员工工号", ref _员工工号, value);
                if (!IsLoading && !IsSaving)
                {

                    UserName = this.员工工号;


                }
            }
        }




        public 岗位 岗位
        {
            get
            {
                return _岗位;
            }
            set
            {
                SetPropertyValue("岗位", ref _岗位, value);
            }
        }
        [Association("部门-Sys_User")]
        public 部门 部门
        {
            get
            {
                return _部门;
            }
            set
            {
                SetPropertyValue("部门", ref _部门, value);


            }
        }

        private string _姓名;
        private string _员工工号;
        private string _手机号;
        private string _邮件地址;

        [Size(SizeAttribute.DefaultStringMappingFieldSize)]
        public string 分机号码
        {
            get
            {
                return _分机号码;
            }
            set
            {
                SetPropertyValue("分机号码", ref _分机号码, value);
            }
        }

        [Size(SizeAttribute.DefaultStringMappingFieldSize)]
        public string 短号
        {
            get
            {
                return _短号;
            }
            set
            {
                SetPropertyValue("短号", ref _短号, value);
            }
        }

        [Size(SizeAttribute.DefaultStringMappingFieldSize)]
        public string 姓名
        {
            get
            {
                return _姓名;
            }
            set
            {
                SetPropertyValue("姓名", ref _姓名, value);
            }
        }

        [Size(SizeAttribute.DefaultStringMappingFieldSize)]
        public string 邮件地址
        {
            get
            {
                return _邮件地址;
            }
            set
            {
                SetPropertyValue("邮件地址", ref _邮件地址, value);
            }
        }

        [Size(SizeAttribute.DefaultStringMappingFieldSize)]
        public string 手机号
        {
            get
            {
                return _手机号;
            }
            set
            {
                SetPropertyValue("手机号", ref _手机号, value);
            }
        }



        #region IPermissionPolicyUser Members
        IEnumerable<IPermissionPolicyRole> IPermissionPolicyUser.Roles
        {
            get { return Sys_Roles.OfType<IPermissionPolicyRole>(); }
        }
        #endregion

        #region ICanInitialize Members
        void ICanInitialize.Initialize(IObjectSpace objectSpace, SecurityStrategyComplex security)
        {
            Sys_Role newUserRole = (Sys_Role)objectSpace.FindObject<Sys_Role>(
                new BinaryOperator("Name", security.NewUserRoleName));
            if (newUserRole == null)
            {
                newUserRole = objectSpace.CreateObject<Sys_Role>();
                newUserRole.Name = security.NewUserRoleName;
                newUserRole.IsAdministrative = true;
                newUserRole.Sys_Users.Add(this);
            }
        }
        #endregion

    }
}